package com.mbl.base.core.utils;

import io.jsonwebtoken.ExpiredJwtException;
import org.apache.commons.lang3.StringUtils;

/**
 * Created with IntelliJ IDEA.
 * User: lwj
 * Date: 2020/3/26
 * Time: 12:56
 * To change this template use File | Settings | File Templates.
 * Description:
 */
public class UtilToken {
    private static final String tokenHeader = "mbl_token";

    /**
     * 根据token获取username
     * @return
     */
    public static String getUserName(){
        String username = null;
        String authToken = getToken();
        if(StringUtils.isBlank(authToken)){
            return username;
        }
        try {
            username = UtilJwtToken.getUsernameFromToken(authToken);
        } catch (ExpiredJwtException e) {
        }
        return username;
    }

    /**
     * 获取token
     * @return
     */
    public static String getToken(){
        final String requestHeader = UtilHttp.getRequest().getHeader(tokenHeader);
        String authToken = null;
        if (requestHeader != null && requestHeader.startsWith("mbl ")) {
            authToken = requestHeader.substring(4);
        }
        return authToken;
    }
}
